#include <algorithm>

#include "adr/BaseModel.h"
#include "adr/Metadata/BaseProperty.h"

namespace adr {
	using namespace std;

	BaseModel::~BaseModel() {}

	bool BaseModel::IsValid() const {
		return !any_of(
			begin(this->properties),
			end(this->properties),
			[&](BaseProperty* prop) {
			return !prop->IsValid();
		});
	}

	bool BaseModel::Validate() {
		bool isValid = true;

		for_each(
			begin(this->properties),
			end(this->properties),
			[&](BaseProperty* prop) {
			isValid = prop->Validate() && isValid;
		});

		return isValid;
	}

	vector<string> BaseModel::GetValidationErrors() {
		vector<string> result;

		for_each(
			begin(this->properties),
			end(this->properties),
			[&](BaseProperty* prop) {
			if (!prop->IsValid()) {
				auto errors = prop->GetValidationErrors();
				for_each(
					begin(errors),
					end(errors),
					[&](const string& error) {
					result.emplace_back(error);
				});					
			}
		});

		return result;
	}
}